/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FilterSelectionDialog.java
 *
 * Created on May 29, 2011, 2:03:51 PM
 */

package org.openbookformat.obffinder.filters;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Observable;
import java.util.Observer;
import javax.swing.DefaultListModel;

/**
 *
 * @author matamh0a
 */
public class FilterSelectionDialog extends javax.swing.JDialog {

    private boolean okSelected = false;
    private Filter selectedFilter = null;

    /** Creates new form FilterSelectionDialog */
    public FilterSelectionDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        setTitle("Filter Selection Dialog ...");
        model = new DefaultListModel();
        initComponents();
        ArrayList<Filter> filters = FiltersManager.getInstance().getFilters();
        Collections.sort(filters);
        for(int i = 0; i < filters.size(); i++) {
            model.addElement(filters.get(i));
        }
        jFilterList.setModel(model);

        observable = new MyObservable();
    }

    public boolean isOkSelected() {
        return okSelected;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jFilterText = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jFilterList = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jFilterDesc = new javax.swing.JEditorPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText(org.openide.util.NbBundle.getMessage(FilterSelectionDialog.class, "FilterSelectionDialog.jLabel1.text")); // NOI18N

        jFilterText.setText(org.openide.util.NbBundle.getMessage(FilterSelectionDialog.class, "FilterSelectionDialog.jFilterText.text")); // NOI18N
        jFilterText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jFilterTextKeyTyped(evt);
            }
        });

        jFilterList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jFilterListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jFilterList);

        jButton1.setText(org.openide.util.NbBundle.getMessage(FilterSelectionDialog.class, "FilterSelectionDialog.jButton1.text")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(org.openide.util.NbBundle.getMessage(FilterSelectionDialog.class, "FilterSelectionDialog.jButton2.text")); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jFilterDesc.setEditable(false);
        jScrollPane2.setViewportView(jFilterDesc);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFilterText, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jFilterText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        okSelected = true;
        setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        setVisible(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jFilterListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jFilterListValueChanged
        if(jFilterList.getSelectedIndices().length == 0)
            return;

        Filter filter = (Filter) jFilterList.getSelectedValue();
        jFilterDesc.setText(filter.getDescription());
        selectedFilter = filter;
        observable.resultChanged();
    }//GEN-LAST:event_jFilterListValueChanged

    private void jFilterTextKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jFilterTextKeyTyped
        model.clear();
        jFilterDesc.setText("");
        Filter filter = null;
        if(jFilterText.equals("")) {
            model.clear();
            for(int i = 0; i < FiltersManager.getInstance().getFilters().size(); i++) {
                model.addElement(FiltersManager.getInstance().getFilters().get(i));
            }
        } else {
            for(int i = 0; i < FiltersManager.getInstance().getFilters().size(); i++) {
                filter = FiltersManager.getInstance().getFilters().get(i);
                if(filter.getName().contains(jFilterText.getText())
                        || filter.getDescription().contains(jFilterText.getText()))
                    model.addElement(filter);
            }
        }
    }//GEN-LAST:event_jFilterTextKeyTyped


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FilterSelectionDialog dialog = new FilterSelectionDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }


    public void addObserver(Observer o) {
        observable.addObserver(o);
    }

    public void deleteObserver(Observer o) {
        observable.deleteObserver(o);
    }

    MyObservable observable;
    class MyObservable extends Observable {

        void resultChanged() {
            super.setChanged();
            super.notifyObservers(selectedFilter);
        }
    }

    public Filter getSelectedFilter() {
        return selectedFilter;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JEditorPane jFilterDesc;
    private javax.swing.JList jFilterList;
    private javax.swing.JTextField jFilterText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

    private DefaultListModel model;
}
